import moment from 'moment';
//表单类型枚举
export const FORM_TYPE_LIST = {
  1: {
    name: '文本框',
    formType: 'FormText',
    max: '字段最大长度',
    min: true,
    regEx: true,
  },
  2: {
    name: '数字文本框',
    formType: 'FormText',
    max: '最大数',
    min: true,
    precision: true,
  },
  3: {
    name: '密码文本框',
    formType: 'FormText',
    max: '字段最大长度',
    min: true,
    regEx: true,
  },
  4: {
    name: '文本域',
    formType: 'FormText',
    max: '字段最大长度',
    min: true,
    regEx: true,
  },
  5: {
    name: '下拉选择框',
    formType: 'FormSelect',
    fieldOption: true,
    multiple: true,
  },
  6: {
    name: '单选框',
    formType: 'FormRadio',
    fieldOption: true,
  },
  7: {
    name: '多选框',
    formType: 'FormCheckbox',
    fieldOption: true,
  },
  8: {
    name: '时间选择器',
    formType: 'FormDate',
    dateType: true,
  },
  9: {
    name: '时间范围选择器',
    formType: 'FormDateRange',
    dateType: true,
  },
  10: {
    name: '上传组件',
    formType: 'FormUpload',
    max: '可上传数',
  },
  100: {
    name: '省市级联框',
    formType: 'PresetForm',
    // multiple: true,
  },
  101: {
    name: '用户部门树形框',
    formType: 'PresetForm',
    multiple: true,
  },
  102: {
    name: '角色选择框',
    formType: 'PresetForm',
    multiple: true,
  },
  103: {
    name: '岗位选择框',
    formType: 'PresetForm',
    multiple: true,
  },
  toOption: function () {
    return Object.keys(this).map((item) => {
      return { ...this[item], label: this[item].name, value: parseInt(item) };
    });
  },
  toGroupOption: function () {
    let option = this.toOption();
    return [
      {
        label: '自定义表单类型',
        value: '自定义表单类型',
        optionType: 'optGroup',
        children: option.filter((item) => {
          return item.formType !== 'PresetForm';
        }),
      },
      {
        label: '预设表单类型',
        value: '预设表单类型',
        optionType: 'optGroup',
        children: option.filter((item) => {
          return item.formType === 'PresetForm';
        }),
      },
    ];
  },
  toEnum: function () {
    let _this = {};
    Object.keys(this).map((item) => {
      _this[item] = {
        text: this[item].name,
      };
    });
    return _this;
  },
  parseData: function (data: any, fieldType: any) {
    let formType = this[fieldType].formType;
    switch (formType) {
      case 'FormText':
        try {
          return JSON.parse(data);
        } catch (error) {
          return '';
        }

      case 'FormDate':
        try {
          return moment(JSON.parse(data));
        } catch (error) {
          return moment();
        }

      case 'FormDateRange':
        try {
          let Range = JSON.parse(data);
          Range?.map((item: any, index: number) => {
            Range[index] = moment(item);
          });
          return Range;
        } catch (error) {
          [moment(), moment()];
        }

      case 'FormUpload':
        try {
          let _data = JSON.parse(data);
          return Array.isArray(_data) ? _data : [];
        } catch (error) {
          return [];
        }

      default:
        try {
          return JSON.parse(data);
        } catch (error) {
          return '';
        }
    }
  },
  toCols: function (field: any) {
    const { id, fieldType, label } = field;
    let formType = this[fieldType].formType;
    let dataIndex = `custom_${id}_${fieldType}`;
    switch (formType) {
      case 'FormText':
        return {
          title: label,
          dataIndex,
          hideInSearch: true,
          render: (_: any, record: any) => {
            try {
              return JSON.parse(record[dataIndex]);
            } catch (error) {
              return '';
            }
            return '-';
          },
        };
      case 'FormDate':
        return {
          title: label,
          dataIndex,
          hideInSearch: true,
          render: (_: any, record: any) => {
            try {
              return JSON.parse(record[dataIndex]);
            } catch (error) {
              return '-';
            }
            return '-';
          },
        };

      case 'FormDateRange':
        return {
          title: label,
          dataIndex,
          hideInSearch: true,
          render: (_: any, record: any) => {
            try {
              let Range = JSON.parse(record[dataIndex]);
              Range?.map((item: any, index: number) => {
                Range[index] = moment(item);
              });
              return Range[0] + '-' + Range[1];
            } catch (error) {
              return '-';
            }
            return '-';
          },
        };
      case 'FormSelect' || 'FormRadio' || 'FormCheckbox':
        return {
          title: label,
          dataIndex,
          hideInSearch: true,
          render: (_: any, record: any) => {
            try {
              let data = JSON.parse(record[dataIndex]);
              let str = '-';
              if (Array.isArray(data)) {
                let strArr = data.map((item: any) => {
                  // return item.label;
                  return item;
                });
                str = strArr.join('，');
              } else {
                // str = data.label;
                str = data;
              }
              return str;
            } catch (error) {
              return '-';
            }
            return '-';
          },
        };

      case 'FormUpload':
        return null;

      default:
        return {
          title: label,
          dataIndex,
          hideInSearch: true,
          render: (_: any, record: any) => {
            try {
              return JSON.parse(record[dataIndex]);
            } catch (error) {
              return '';
            }
            return '-';
          },
        };
    }
  },
};
['toOption', 'toEnum', 'parseData', 'toGroupOption', 'toCols'].map((item) => {
  Object.defineProperty(FORM_TYPE_LIST, item, {
    enumerable: false,
  });
});

//正则枚举
export const REG_EX_LIST = {
  phone: {
    value: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
    name: '手机',
  },
  idCard: {
    pattern:
      /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
    name: '身份证',
    message: '身份证格式不正确',
  },
  email: {
    pattern: /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/,
    name: '邮箱',
    message: '邮箱格式不正确',
  },
  url: {
    pattern: /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/,
    name: 'URL',
    message: 'URL格式不正确',
  },
  qq: {
    pattern: /^[1-9][0-9]{4,10}$/,
    name: 'QQ号',
    message: 'QQ号格式不正确',
  },
  postalCode: {
    pattern: /[1-9]\d{5}(?!\d)/,
    name: '邮政编码',
    message: '邮政编码格式不正确',
  },
  toOption: function () {
    return Object.keys(this).map((item) => {
      return { ...this[item], label: this[item].name, value: item };
    });
  },
};
['toOption'].map((item) => {
  Object.defineProperty(REG_EX_LIST, item, {
    enumerable: false,
  });
});

//时间控件类型
export const DATE_TYPE_LIST = {
  time: {
    name: '小时',
  },
  date: {
    name: '天',
  },
  dateTime: {
    name: '天+小时',
  },
  week: {
    name: '周',
  },
  month: {
    name: '月',
  },
  quarter: {
    name: '季度',
  },
  year: {
    name: '年',
  },
  toOption: function () {
    return Object.keys(this).map((item) => {
      return { ...this[item], label: this[item].name, value: item };
    });
  },
};
['toOption'].map((item) => {
  Object.defineProperty(DATE_TYPE_LIST, item, {
    enumerable: false,
  });
});

export const IMG_PREFIX: string & any = process.env.imgPrefix;
export const BlOG_API_URL: string & any = process.env.blogApiUrl;
